<template>
    <div
        class="uploader-box"
        :class="box1HoverStyle"
        @click="selectFile"
        @drop="drop"
        @dragleave="dragLeave"
        @dragover="dragOver"
    >
        <div class="uploader-box2" :class="box2HoverStyle">
            <input
                type="file"
                :accept="accept"
                :multiple="multiple"
                ref="input"
                hidden
                @change="fileValueChange"
            />
            <slot></slot>
        </div>
    </div>
</template>

<script>
export default {
  name: 'DragUploadFile',
  props: {
    accept: {
      type: String,
      default: null
    },
    multiple: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      isHover: false
    }
  },
  computed: {
    box1HoverStyle () {
      return this.isHover ? 'uploader-box-hover' : ''
    },
    box2HoverStyle () {
      return this.isHover ? 'uploader-box2-hover' : ''
    }
  },
  mounted () {},
  methods: {
    drop (event) {
      // 禁止默认动作，防止浏览器直接打开文件
      this.isHover = false
      event.preventDefault()
      const files = event.dataTransfer.files
      this.$emit('file', files)
    },
    dragOver (event) {
      // 禁止默认动作，防止浏览器直接打开文件
      event.preventDefault()
      this.isHover = true
    },
    dragLeave () {
      this.isHover = false
    },
    // 选择文件
    selectFile () {
      this.$refs.input.click()
    },
    fileValueChange () {
      const files = this.$refs.input.files
      this.$emit('file', files)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.uploader-box {
    display: block;
    border: 1px solid var(--Border2);
    width: 200px;
    height: 200px;
    border-radius: 20px;
    position: relative;
    transition: all 0.3s ease-in-out;
    color: var(--Text);
    box-shadow: var(--shadow);
    cursor: pointer;
}

.uploader-box2 {
    position: absolute;
    left: 10px;
    top: 10px;
    right: 10px;
    bottom: 10px;
    border: 1px dashed var(--Border1);
    border-radius: 13px;
    word-break: normal;
    padding: 10px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    transition: all 0.3s ease-in-out;
}

.uploader-box-hover {
    transform: scale(1.05);
    box-shadow: var(--shadow-hover);
}
.uploader-box:hover {
    @extend .uploader-box-hover;
}

.uploader-box2-hover {
    border: 1px dashed var(--MainColor);
    color: var(--MainColor);
}
.uploader-box2:hover {
    @extend .uploader-box2-hover;
}
</style>
